#!/bin/sh
# System startup script for moonraker 3d-printer host code

### BEGIN INIT INFO
# Provides:          moonraker
# Required-Start:    $local_fs
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: moonraker daemon
# Description:       Starts the moonraker daemon.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DESC="moonraker daemon"
NAME="moonraker"
DEFAULTS_FILE=/etc/default/moonraker
PIDFILE=/var/run/moonraker.pid
function status_msg() {
  echo -e "\n${magenta}## ${1}${white}"
}
# Read defaults file
[ -r $DEFAULTS_FILE ] && . $DEFAULTS_FILE

case "$1" in
start)  status_msg "启动moonraker"
        start-stop-daemon --start --quiet --exec $MOONRAKER_EXEC  --background --pidfile $PIDFILE --make-pidfile -- $MOONRAKER_ARGS
        status_msg "启动完成"
        ;;
stop)   status_msg "停止moonraker"
        nginx -s stop
        kill -9 $(cat $PIDFILE)
        RETVAL=$?
        [ $RETVAL -eq 0 ] && [ -e "$PIDFILE" ] && rm -f $PIDFILE
        status_msg $RETVAL
        ;;
restart) status_msg "重新启动moonraker"
        $0 stop
        $0 start
        ;;
esac
exit 0